package com.kayb.patterns.behavioral;

/**
 * 命令者模式，为了解决命令请求者与命令实现这之间的耦合问题。
 * @author huangkaibin
 * @date 2018-08-28
 **/
public class Command {

    interface IReceiver {
        void execute();
    }

    interface ICommand {
        void execute();
    }

    static class Receiver1 implements IReceiver {

        @Override
        public void execute() {
            System.out.println("Receiver1 execute");
        }
    }

    static class Receiver2 implements IReceiver {

        @Override
        public void execute() {
            System.out.println("Receiver2 execute");
        }
    }

    static class Command1 implements ICommand {
        private IReceiver receiver;

        public Command1(IReceiver receiver) {
            this.receiver = receiver;
        }

        @Override
        public void execute() {
            System.out.println("command1 executed.");
            receiver.execute();
        }
    }

    static class Command2 implements ICommand {
        private IReceiver receiver;

        public Command2(IReceiver receiver) {
            this.receiver = receiver;
        }

        @Override
        public void execute() {
            System.out.println("command2 executed.");
            receiver.execute();
        }
    }

    static class Invoker {
        public void action(ICommand command) {
            command.execute();
        }
    }

    public static void main(String[] args) {
        IReceiver receiver = new Receiver1();
        ICommand command = new Command1(receiver);
        Invoker invoker = new Invoker();
        invoker.action(command);
    }
}
